package cn.iocoder.yudao.module.pinche.controller.admin.pincorder;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.pinche.controller.admin.pincorder.vo.*;
import cn.iocoder.yudao.module.pinche.dal.dataobject.pincorder.PinCheOrderDO;
import cn.iocoder.yudao.module.pinche.convert.pincorder.PinCheOrderConvert;
import cn.iocoder.yudao.module.pinche.service.PinCheOrderService;

@Tag(name = "管理后台 - 拼车信息")
@RestController
@RequestMapping("/pinche/pin-che-order")
@Validated
public class PinCheOrderController {

    @Resource
    private PinCheOrderService pinCheOrderService;

    @PostMapping("/create")
    @Operation(summary = "创建拼车信息")
    @PreAuthorize("@ss.hasPermission('pinche:pin-che-order:create')")
    public CommonResult<Long> createPinCheOrder(@Valid @RequestBody PinCheOrderCreateReqVO createReqVO) {
        return success(pinCheOrderService.createPinCheOrder(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新拼车信息")
    @PreAuthorize("@ss.hasPermission('pinche:pin-che-order:update')")
    public CommonResult<Boolean> updatePinCheOrder(@Valid @RequestBody PinCheOrderUpdateReqVO updateReqVO) {
        pinCheOrderService.updatePinCheOrder(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除拼车信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('pinche:pin-che-order:delete')")
    public CommonResult<Boolean> deletePinCheOrder(@RequestParam("id") Long id) {
        pinCheOrderService.deletePinCheOrder(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得拼车信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('pinche:pin-che-order:query')")
    public CommonResult<PinCheOrderRespVO> getPinCheOrder(@RequestParam("id") Long id) {
        PinCheOrderDO pinCheOrder = pinCheOrderService.getPinCheOrder(id);
        return success(PinCheOrderConvert.INSTANCE.convert(pinCheOrder));
    }

    @GetMapping("/list")
    @Operation(summary = "获得拼车信息列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('pinche:pin-che-order:query')")
    public CommonResult<List<PinCheOrderRespVO>> getPinCheOrderList(@RequestParam("ids") Collection<Long> ids) {
        List<PinCheOrderDO> list = pinCheOrderService.getPinCheOrderList(ids);
        return success(PinCheOrderConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得拼车信息分页")
    @PreAuthorize("@ss.hasPermission('pinche:pin-che-order:query')")
    public CommonResult<PageResult<PinCheOrderRespVO>> getPinCheOrderPage(@Valid PinCheOrderPageReqVO pageVO) {
        PageResult<PinCheOrderDO> pageResult = pinCheOrderService.getPinCheOrderPage(pageVO);
        return success(PinCheOrderConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出拼车信息 Excel")
    @PreAuthorize("@ss.hasPermission('pinche:pin-che-order:export')")
    @OperateLog(type = EXPORT)
    public void exportPinCheOrderExcel(@Valid PinCheOrderExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<PinCheOrderDO> list = pinCheOrderService.getPinCheOrderList(exportReqVO);
        // 导出 Excel
        List<PinCheOrderExcelVO> datas = PinCheOrderConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "拼车信息.xls", "数据", PinCheOrderExcelVO.class, datas);
    }

}
